from . import app_send_sms
from . import SendSms
from flask import request
from conn_mysql import ConnectMysql


@app_send_sms.route('/sms')
def sms():

    phone = request.args.get('phone')
    openid = request.args.get('openid')

    if not phone or len(phone) != 11 or not openid:

        return {
            "status": -1,
            "errorCode": 1,
            "errorMsg": '参数不正确',
            "resultBody": None
        }

    sms_obj = SendSms(phone)

    with ConnectMysql() as c:

        sql = "INSERT INTO phone_notify_code(phone_notify_code, openid) values (%s,%s)"

        c.insert_info(sql, (sms_obj.code, openid))

    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": "短信已发送"

    }



    